﻿using Itech_Run.Application.Shared;
using Itech_Run.Blog.Application.Contracts.Dto;
using Itech_Run.Blog.Application.Contracts.Service;
using Itech_Run.Blog.Domain.Entity;
using System.Linq.Expressions;
using System.Linq.Dynamic.Core;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Itech_Run.Blog.Application.Service
{
    public class Front_BlogInfoService : BaseApplicationService<Blog_Info>, IFront_BlogInfoService
    {
        private readonly IRepository<Blog_Info, string> _blogInfoRepository;
        private readonly IRepository<Blog_Interest, string> _blogInterestRepository;

        public Front_BlogInfoService(IRepository<Blog_Info, string> blogInfoRepository, IRepository<Blog_Interest, string> blogInterestRepository)
        {
            _blogInfoRepository = blogInfoRepository;
            _blogInterestRepository = blogInterestRepository;
        }
        public async Task<PagedResultDto<Front_BlogInfoDto>> GetListAsync(Front_BlogInfoListDto listDto)
        {
            string topSort = "IsTop desc,";//置顶
            if (string.IsNullOrEmpty(listDto.Sorting)) listDto.Sorting = "CreatedTime desc";
            listDto.Sorting = topSort + listDto.Sorting;
            Expression<Func<Blog_Info, bool>> curExpression = s => s.IsPublish == true;
            if (!string.IsNullOrEmpty(listDto.Title))
                curExpression = curExpression.And(s => s.Title.Contains(listDto.Title));
            if (!string.IsNullOrEmpty(listDto.PersonCategoryId))
                curExpression = curExpression.And(s => s.Blog_PersonCategory_Map.Any(p => p.PersonCategoryId == listDto.PersonCategoryId));
            if (!string.IsNullOrEmpty(listDto.PersonLabelId))
                curExpression = curExpression.And(s => s.Blog_PersonLabel_Map.Any(p => p.PersonLabelId == listDto.PersonLabelId));

            var query = (await _blogInfoRepository.WithDetailsAsync()).WhereIf(true, curExpression).OrderBy(listDto.Sorting);
            var items = ObjectMapper.Map<List<Blog_Info>, List<Front_BlogInfoDto>>(query.PageBy(listDto.SkipCount, listDto.MaxResultCount).ToList());

            return new PagedResultDto<Front_BlogInfoDto>(query.Count(), items);
        }

        public async Task<Front_BlogInfoDto> GetAsync(string id)
        {
            var blogInfo = await _blogInfoRepository.GetAsync(id, includeDetails: true);
            return ObjectMapper.Map<Blog_Info, Front_BlogInfoDto>(blogInfo);
        }

        public async Task AddReadNum(string blogId)
        {
            if (await _blogInterestRepository.AnyAsync(p => p.BlogId == blogId))
            {
                var blogInterest = await _blogInterestRepository.GetAsync(p => p.BlogId == blogId);
                blogInterest.ReadNum += 1;
                await _blogInterestRepository.UpdateAsync(blogInterest);
            }
            else
            {
                var blogInterest = new Blog_Interest
                {
                    BlogId = blogId,
                    ReadNum = 1
                };
                SetInsertBy(blogInterest, null);
                await _blogInterestRepository.InsertAsync(blogInterest);
            }
        }
    }
}
